package com.example.demo.configuration

import android.content.Context
import android.util.Log
import com.example.featuresdk.core.EnvConfiguration
import com.google.gson.Gson
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class ConfigurationProvider {

    companion object{
        const val TAG = "ConfigurationProvider"
    }

    @Singleton
    @Provides
    fun envConfiguration(@ApplicationContext context: Context): EnvConfiguration {
        runCatching{
            val reader = context.assets.open("environment.json").reader()
            Gson().fromJson(reader, EnvConfiguration::class.java)
        }.onFailure {
            Log.e(TAG,"environment",it)
            throw it
        }.onSuccess {
            Log.i(TAG,"hostname=${it.hostname}")
            EnvConfiguration.instance = it
        }

        return EnvConfiguration.instance
    }
}